﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace WebAPI.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class WeatherForecastController : ControllerBase
    {

        private static OrderService orderService = new OrderService();


        [HttpGet]
        public OrderResponse Get([FromBody] RequestOrder req)
        {
            OrderResponse orderResponse = new OrderResponse();

            try
            {
                orderResponse.orderID = req.orderID;
                var queryAns = from order in orderService.orders where order.orderID == req.orderID orderby order.orderDetail.orderAmount select order;
                foreach (Order order in queryAns)
                {
                    orderResponse.customer = order.orderDetail.customer;
                    orderResponse.productName = order.orderDetail.productName;
                    orderResponse.productPrice = order.orderDetail.orderAmount;
                    break;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }


            return orderResponse;
        }

        [HttpPost]

        public OrderAddResponse Post([FromBody] OrderAddRequest req)
        {
            OrderAddResponse orderResponse = new OrderAddResponse();

            try
            {
                Console.WriteLine(req.isModify);
                if (!req.isModify)
                {
                    Order t1 = new Order(req.orderID, req.productName, req.customer, req.productPrice);
                    orderService.addOrder(t1);
                }
                else
                {
                    orderService.modifyOrder(req.orderID, req.productName, req.customer, req.productPrice);
                }
                orderResponse.status = "success";
                orderResponse.orderID = req.orderID;
                orderResponse.message = "no error";
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                orderResponse.status = "fail";
                orderResponse.orderID = req.orderID;
                orderResponse.message = ex.Message;
            }


            return orderResponse;
        }


        [HttpDelete]

        public OrderDeleteResponse Delete([FromBody] OrderDeleteRequest req)
        {
            OrderDeleteResponse orderResponse = new OrderDeleteResponse();

            try
            {

                orderService.deleteOrder(req.orderID);
                orderResponse.status = "success";
                orderResponse.orderID = req.orderID;
                orderResponse.message = "no error";
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                orderResponse.status = "fail";
                orderResponse.orderID = req.orderID;
                orderResponse.message = ex.Message;
            }


            return orderResponse;
        }



    }
}
